package com.dao;

import java.util.List;

import com.model.DoorDetail;
import com.model.DoorOrder;
import com.model.Employee;
import com.model.Orders;
import com.model.Product;
import com.util.JdbcUtil;

public class EmployeeDao {
	JdbcUtil jdbc = new JdbcUtil();
	
	//修改个人信息
	public Employee update(Employee e ,String ephone,String eaddress) throws Exception {
		String sql = "update employee set ephone=?,eaddress=? where eid = ?";
		jdbc.updatePreparedStatement(sql, ephone,eaddress,e.getEid());
		sql = "select * from employee where eid = ?";
		List<Employee> employees = jdbc.queryPreparedStatement(sql, Employee.class, e.getEid());
		Employee employee = employees.get(0);
		return employee;
	}
	
	//检查原密码是否正确
	public boolean checkpass(String eid,String oldpass){
		String sql ="select * from employee where eid=?,epassword = ?";
		List<Employee> employee = jdbc.queryPreparedStatement(sql, Employee.class, eid,oldpass);
		if(employee.size()>0){
			return true;
		}
		return false;
	}
	//修改密码
	public void changepass(Integer eid, String oldpass, String newpass) throws Exception{
		StringBuffer sql =new StringBuffer();
		sql.append("update employee set epassword = ");
		sql.append(newpass);
		sql.append(" where eid=");
		sql.append(eid);
		jdbc.updatePreparedStatement(sql.toString());
	}
	
	//查询自己工作的上门预约的订单
	public  List<DoorOrder> query(Integer eid,String uname){
		StringBuffer sql = new StringBuffer();
		List<DoorOrder> doors =null;
		sql.append("select user.uid,user.uname,user.uphone,user.uaddress,orders.destinetime,orders.oid ");
		sql.append("from user,orders ");
		sql.append("where user.uid = orders.uid and orders.okind = 'tohome' and orders.eid= ?  ");
		if(uname.equals("")||uname==null){
			doors = jdbc.queryPreparedStatement(sql.toString(), DoorOrder.class,eid.toString());
		}else{
			sql.append(" and user.uname= ? ");
			doors = jdbc.queryPreparedStatement
					(sql.toString(), DoorOrder.class,eid.toString(), uname.toString());
		}
		for(DoorOrder door :doors){
			System.out.println(door.getDestinetime());
		}
		
		return doors;
	}
	
	//查看预约上门订单详细情况
	public List<DoorDetail> queryDetail(Integer eid,String uname){
		List<DoorDetail> details = null;
		StringBuffer sql = new StringBuffer();
		//sql.append("select user.uid,user.uname,orderitem.pname,orderitem.pprice,orderitem.weight,orderitem.tprice ");
		sql.append("select orderitem.*,user.uname ");
		sql.append(" from orderitem,user ");
		sql.append(" where user.uid=orderitem.uid and orderitem.eid= ? ");
		if(uname.equals("")||uname==null){
			details = jdbc.queryPreparedStatement(sql.toString(), DoorDetail.class,eid.toString());
		}else{
			sql.append(" and orderitem.oid= ? ");
			details = jdbc.queryPreparedStatement
					(sql.toString(), DoorDetail.class,eid.toString(), uname.toString());
		}
		return details;
	}

	//删除
	public void del(String oid, Integer pid,Double tprice) throws Exception {
		String sql ="delete  from orderitem where oid=? and pid = ? ";
		jdbc.updatePreparedStatement(sql, oid,pid);
		sql = "update orders set total = total - ? where oid = ?";
		jdbc.updatePreparedStatement(sql, tprice,oid);
	}
	
	//保存
	public void save(String oid, Integer pid, Double weight, Double tprice,Double oldtprice) throws Exception{
		
		String sql ="update  orderitem set weight = ?,tprice=? where oid=? and pid = ? ";
		jdbc.updatePreparedStatement(sql, weight,tprice,oid,pid);
		//修改总价格，先删除原先的价格再加上后来的总价
		sql = "update orders set total = total-?+? where oid = ? ";
		jdbc.updatePreparedStatement(sql, oldtprice,tprice,oid);
	}
	//添加
	public void add(DoorDetail detail, Integer eid) throws Exception {
		String sb="select * from product where pname = ?";
		List<Product> product = jdbc.queryPreparedStatement(sb, Product.class, detail.getPname());
		String sql = "insert into orderitem values(?,?,?,?,?,?,?,?,?,?)";
		jdbc.updatePreparedStatement(sql, detail.getOiid(),detail.getOid(),detail.getUid(),
				product.get(0).getPid(),eid,detail.getPname(),detail.getPimage(),
		detail.getPprice(),detail.getWeight(),detail.getTprice());
		//修改订单中的总价格
		sql = "update orders set total = total+ ? where oid = ?";
		jdbc.updatePreparedStatement(sql, detail.getTprice(),detail.getOid());
	}
	
	
	
	//查询所有的product
	public List<Product> allproducts() {
		String sql ="select * from product";
		List<Product> product = jdbc.queryPreparedStatement(sql, Product.class);
		return product;
	}
	//查询某订单的用户
	public Orders order(String oid) {
		String sql="select * from orders where oid =?";
		List<Orders> orders = jdbc.queryPreparedStatement(sql, Orders.class, oid);
		return orders.get(0);
	}

	public Double returnPrice(String pname) {
		String sql ="select * from product where pname= ?";
		List<Product> product = jdbc.queryPreparedStatement(sql, Product.class,pname);
		return product.get(0).getPprice();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
